home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14709 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: newsfeed.internetmci.com!xmission!news
  2. From: macron@xmission.com (Joe Schlimgen)
  3. Newsgroups: comp.lang.c++
  4. Subject: auto_ptr capability question
  5. Date: Mon, 01 Apr 1996 17:17:51 GMT
  6. Organization: XMission Internet (801 539 0900)
  7. Message-ID: <31600aa3.1288774@news.xmission.com>
  8. NNTP-Posting-Host: slc43.xmission.com
  9. X-Newsreader: Forte Agent .99d/32.182
  10.  
  11. Before the STL, I created my own auto_ptr like class. Now that I have
  12. the STL (provided with BC++ 5.0), I've decided to use the standard
  13. auto_ptr.
  14.  
  15. My class has a conversion from auto_ptr<X> to X* (shown below). This
  16. made it very easy to use an auto_ptr just like a pointer. The STL does
  17. not provide an implicit conversion, you must use the get() member
  18. function.
  19.  
  20. Can anyone provide a reason why the designers of the STL (who, I would
  21. think, are much more knowledgeable about C++ and OOP than I) would
  22. choose to provide an explicit rather than an implicit function? I see no
  23. potential problems with an implicit conversion (but I could very well be
  24. wrong).
  25.  
  26. Also, I've noticed that the assignment from another auto_ptr function
  27. (also shown below) causes the auto_ptr being assign *from* to release
  28. ownership (as it should) but does *not* delete the (possible) pointer
  29. that is being assigned *to*, thus possibly causing a memory leak.
  30.  
  31. Is this a bug, or is it intentional? If intentional, why?
  32.  
  33. Pertinent parts of the auto_ptr class:
  34.  
  35. template<class X> class auto_ptr
  36.     {
  37.     // ...
  38.  
  39.   X* get () const { return the_p; }                    // Provided by the STL
  40.     X* operator X* () const { return the_p; }    // Not provided by the STL
  41.  
  42.   // ...
  43.  
  44.     void operator= (auto_ptr<X>& rhs) { reset(rhs.release()); }
  45.     X* reset (X* p = 0) { X* tmp = the_p; the_p = p; return tmp; }
  46.  
  47.   // ...
  48.   }
  49.  
  50.  
  51.  
  52. -- The Truth Is Out There --
  53.